package com.xcity.game.item.effect;

import java.text.MessageFormat;

import com.xcity.db.entity.Player;
import com.xcity.game.common.Cause;
import com.xcity.game.common.MessageConst;
import com.xcity.game.employee.EmployeeQuality;
import com.xcity.game.employee.EmployeeService;
import com.xcity.game.employee.config.impl.EmployeeTemplateConfigure;
import com.xcity.game.item.GameItem;
import com.xcity.game.item.ItemEffect;
import com.xcity.game.item.ItemException;
import com.xcity.game.item.listener.BagListener;
import com.xcity.game.player.PlayerUtils;

import naga.x.App;

/**
 * 按品质掉落一个员工
 * @author yang.li
 *
 */
public class DropEmployeeByQualityEffect implements ItemEffect {

	@Override
	public boolean effect(Player player, GameItem item, int count, BagListener listener) throws ItemException {
		EmployeeQuality quality = EmployeeQuality.forCode(item.getTemplate().getParameter(0));
		if (quality == null) {
			return false;
		}
		int employeeMax = PlayerUtils.getEmployeeCapcity(player);
		if (employeeMax > 0 && player.getEmployees().size() >= employeeMax) {
			throw new ItemException(MessageFormat.format(MessageConst.EMPLOYEE_COUNT_EXCEED, employeeMax));
		}
		EmployeeService service = App.getApp().getServiceManager().get(EmployeeService.class);
		EmployeeTemplateConfigure builder = service.configure().getTemplateConfigure();
		while (--count >= 0) {
			builder.dropByQuality(player, quality, Cause.USE_ITEM);
		}
		return true;
	}

}
